//
//  WXLSongsListCellViewController.m
//  段子手
//
//  Created by dllo on 16/3/28.
//  Copyright © 2016年 JokesTeam. All rights reserved.
//

#import "WXLSongsListCellViewController.h"

@interface WXLSongsListCellViewController ()<UITableViewDataSource, UITableViewDelegate, UIScrollViewDelegate, WXLHotSongsTableViewCellDelegate>

@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) UIImageView *imagView; /**< 大图 */
@property (nonatomic, retain) UIView *bigView; /**< 放在大图上透明的view */

@property (nonatomic, retain) UIButton *leftReturnButton;

@property (nonatomic, retain) NSMutableArray *dataArr; /**< 数据数组 */

@end

@implementation WXLSongsListCellViewController

CGFloat Height = 200;

- (void)dealloc {
    [_tableView release];
    [_imagView release];
    [_bigView release];
    [_leftReturnButton release];
    [_dataArr release];
    [_strID release];
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.dataArr = [NSMutableArray new];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 子视图的创建
    [self createSubView];
    
    // 数据处理
    [self dataHandelFromUrl];
    
    [self cancelBlackLine];
}



#pragma mark - 子视图的创建
- (void)createSubView {
    [self createTableView];
    [self createTableViewHearder];
    [self createLeftItem];
}

/** 创建返回item */
- (void)createLeftItem {
    self.leftReturnButton = [UIButton buttonWithType:(UIButtonTypeSystem)];
    _leftReturnButton.frame = CGRectMake(10, 30, 30, 30);
    [self.view addSubview:_leftReturnButton];
    [_leftReturnButton setBackgroundImage:[UIImage imageNamed:@"iconfont-fanhui-4"] forState:(UIControlStateNormal)];
    [_leftReturnButton addTarget:self action:@selector(leftReturnItemAction:) forControlEvents:(UIControlEventTouchUpInside)];
}

/** 创建tableView */
- (void)createTableView {
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kUIScreenWidth - 0, kUIScreenHeight - 64) style:(UITableViewStylePlain)];
    [self.view addSubview:_tableView];
    [_tableView release];
    _tableView.contentInset = UIEdgeInsetsMake(Height, 0, 0, 0);
    _tableView.showsVerticalScrollIndicator = NO;
    [_tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    _tableView.backgroundColor = [UIColor clearColor];
    
    _tableView.delegate = self;
    _tableView.dataSource = self;
    
    [_tableView registerClass:[WXLHotSongsTableViewCell class] forCellReuseIdentifier:@"WXLHotSongsTableViewCell"];
}

- (void)createTableViewHearder {
    // 设置大的图片
    self.imagView = [[UIImageView alloc] initWithFrame:CGRectMake(0, -Height, self.view.frame.size.width, Height)];
    [self.tableView addSubview:_imagView];
    [_imagView release];
    _imagView.image = [UIImage imageNamed:@"beijing.jpg"];
}




#pragma mark - tableView Delegate & dataSource
-  (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _dataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    WXLHotSongsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"WXLHotSongsTableViewCell"];
    WXLSongsModel *model = _dataArr[indexPath.row];
    cell.backgroundColor = [UIColor clearColor];
    cell.indexPath = indexPath;
    cell.model = model;
    cell.delegate = self;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 60;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [PLAYMUSIC playMusicWithAvUrl:_dataArr dex:indexPath.row];
    [PLAYMUSIC startPlay];
}



#pragma mark - WXLHotSongsTableViewCellDelegate
- (void)mVButtonAtionClick:(WXLSongsModel *)songsModel {
    WXLPLAYVIDEO.songsModel = songsModel;

    [PLAYMUSIC pausePlay];
    
    [self presentViewController:WXLPLAYVIDEO animated:YES completion:nil];
}



#pragma mark - 数据处理
- (void)dataHandelFromUrl {
    NSString *urlStr = [NSString stringWithFormat:@"http://api.songlist.ttpod.com/songlists/%@?address=辽宁省大连市沙河口区五一路314&agent=none&longitude=0.0&net=2&from=android&os=6.0.1&v=v8.4.0.2016022715&alf=700159&api_version=1.0&imei=863360026086235&latitude=0.0&f=168&resolution=1080x1920&language=zh&user_id=0", _strID];
    NSLog(@"%@", urlStr);
    [WxlAPPTools GETWithURL:urlStr par:nil success:^(id responseObject) {
        NSArray *tempArr = [WXLSongsModel modelHandelWithArray:responseObject[@"songs"]];
        if (!tempArr.count) {
            return;
        }
        for (WXLSongsModel *model in tempArr) {
            [self.dataArr addObject:model];
        }
        [_imagView HANDLEIMAGE(responseObject[@"image"][@"pic"])];
        [_tableView reloadData];
    } filed:^(NSError *error) {
        NSLog(@"error:%@", error);
    }];
}



// 取消 黑色
- (void)cancelBlackLine {
    // 取消navigationbar下面的一条素的黑线
    if ([self.navigationController.navigationBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)]) {
        NSArray *list = self.navigationController.navigationBar.subviews;
        for (id obj in list) {
            if ([obj isKindOfClass:[UIImageView class]]) {
                UIImageView *imageView = (UIImageView *)obj;
                NSArray *list2 = imageView.subviews;
                for (id obj2 in list2) {
                    if ([obj2 isKindOfClass:[UIImageView class]]) {
                        UIImageView *imageView2 = (UIImageView *)obj2;
                        imageView2.hidden = YES;
                    }
                }
            }
        }
    }
}


#pragma mark - scrollView Delegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    // tableView滑动时，图片的放大缩小
    CGFloat yOffset = scrollView.contentOffset.y;
    CGFloat xOffset = (yOffset + Height) * 2;
    if (yOffset < -Height) {
        CGRect rect = _imagView.frame;
        rect.origin.y = yOffset;
        rect.size.height = -yOffset;
        rect.origin.x = xOffset;
        rect.size.width = self.view.frame.size.width + fabs(xOffset) * 2;
        
        _imagView.frame = rect;
    }
    //    CGFloat alpha = (yOffset + GroupHeight) / GroupHeight + 0;
    
    UIColor *color = [UIColor colorWithRed:0.42 green:0.33 blue:0.28 alpha:0];
    // 实现navigationBar的滑动隐藏
    [self.navigationController.navigationBar setBackgroundImage:[self imageWithColor:[color colorWithAlphaComponent:0]] forBarMetrics:UIBarMetricsDefault];
}


- (UIImage *)imageWithColor:(UIColor *)color {
    // 描述矩形
    CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
    // 开启位图上下文
    UIGraphicsBeginImageContext(rect.size);
    // 获取位图上下文
    CGContextRef context = UIGraphicsGetCurrentContext();
    // 使用color演示填充上下文
    CGContextSetFillColorWithColor(context, [color CGColor]);
    // 渲染上下文
    CGContextFillRect(context, rect);
    // 从上下文获取图片
    UIImage *theImag = UIGraphicsGetImageFromCurrentImageContext();
    // 结束上下文
    UIGraphicsEndImageContext();
    return theImag;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
